package com.work.libtest2.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.work.libtest2.dto.JobDto;
import com.work.libtest2.entity.Job;
import com.work.libtest2.entity.R;
import com.work.libtest2.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName: JobController
 * Package: com.work.libtest2.controller
 * Description:
 *
 * @Author
 * @Create 2024/6/10 9:11
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/job")
public class JobController {

    @Resource
    private JobService jobService;

    @GetMapping("/getAllJobName")
    public R getAllJobName(){
     List<Job> jobList = jobService.getAllJobName();

     return new R(true,jobList);
    }
    /**
     * 分页查询
     * */
    @GetMapping("/{currentPage}/{pageSize}")
    public R getAllByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, JobDto jobDto){
        IPage<JobDto> page = jobService.getPage(currentPage,pageSize,jobDto);
        if (currentPage>page.getPages()){
            page = jobService.getPage((int)page.getPages(),pageSize,jobDto);
        }
        return new R(true,page);
    }

    /**
     * 添加部门
     * */

    @PostMapping
    public R addJob(@RequestBody Job job){
        int i = jobService.addJob(job);
        return new R(i>0,i>0?"添加成功":"添加失败");
    }

    /**
     * 根据工种id查询获取
     * */

    @GetMapping("/{jobId}")
    public R getJobById(@PathVariable Integer jobId){
        Job job = jobService.getJobById(jobId);
        return new R(true,job);
    }

    /**
     * 修改工种
     * */
    @PutMapping
    public R updateJob(@RequestBody Job job){
        int i = jobService.updateJob(job);
        return new R(i>0);
    }
    /**
     * 删除工种
     * */

    @DeleteMapping("{jobId}")
    public R deleteById(@PathVariable Integer jobId){
        int i = jobService.deleteJobById(jobId);
        return new R(i>0);
    }
}
